//#define WMARecording
#define MP3Recording

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Xivo {
  public partial class RecorderControl : UserControl {
    Xivo.Shared.IXivoRecorder Recorder;

    public RecorderControl() {
      InitializeComponent();

      if (DesignMode == false) {
#if WMARecording
        Recorder = new WMRecorder.RecordModule();
#elif MP3Recording
        Recorder = new WAVMP3Recorder.RecordModule();
#endif
        Xivo.Shared.Common.Radio.onChannelInfoChange += new Xivo.Shared.Delegates.InfoEvent(Radio_onChannelInfoChange);
      }
    }

    void Radio_onChannelInfoChange(Xivo.Shared.ChannelInfo info) {
      Recorder.Info = info;
    }

    //public string RecordSource
    //{
    //  get { return Recorder.RecordSource; }
    //  set { Recorder.RecordSource = value; }
    //}

    private void RecorderControl_Load(object sender, EventArgs e) {
      if (DesignMode == false) {
        //Recorder.OnStateChange += new WMRecorder.RecordModule.StringEvent(Recorder_OnStateChange);
        Recorder.onFilenameChange += new Shared.Delegates.StringEvent(Recorder_FilenameChange);
        ParentForm.FormClosing += new FormClosingEventHandler(ParentForm_FormClosing);
      }
    }

    void ParentForm_FormClosing(object sender, FormClosingEventArgs e) {
      Recorder.StopRecording();
    }

    void Recorder_FilenameChange(string newFilename) {
      if (InvokeRequired)
        Invoke(new Shared.Delegates.StringEvent(Recorder_FilenameChange), new object[] { newFilename });
      else
        lFileName.Text = newFilename;
    }

    void Recorder_OnStateChange(string newState) {
      lStatus.Text = newState;
    }

    private void bStart_Click(object sender, EventArgs e) {
      Start(null, true);
    }

    private void bStop_Click(object sender, EventArgs e) {
      Stop();
    }

    public string Title;
    public string RecordPath {
      get {
        return Recorder.RecordPath;
      }
      set {
        Recorder.RecordPath = value;
      }
    }

    internal void Stop() {
      Recorder.StopRecording();
      //timer1.Enabled = false;
    }

    public bool Recording {
      get {
        return Recorder.Recording;
      }
    }

    internal void Start(Xivo.Shared.ChannelInfo info, bool SplitRecordings) {
      Console.WriteLine("RecorderControl.Start");
      if (Recorder.Recording == false) {
        Recorder.StartRecording(info, SplitRecordings);
        timer1.Enabled = true;
        lStatus.Text = Recorder.State;
      }
    }

    void timer1_Tick(object sender, System.EventArgs e) {
      if (Recorder.Recording)
        lRecordTime.Text = Recorder.RecordTimeStr;
      lStatus.Text = Recorder.State;
    }
  }
}
